package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.AreaDetailDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.AreaDetail;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * Created by bilibili on 2017/4/18.
 */

@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/areaDetail")
public class AreaDetailController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private AreaDetailDao areaDetailDao;


    /**
     * @保存自提地址
     * @param areaDetail
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/saveAreaDetail",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp saveAreaDetail(@RequestBody AreaDetail areaDetail){
        log.debug("保存自提地址");
        areaDetail.setStatus(1);
        areaDetailDao.save(areaDetail);
        return JsonResp.ok();
    }

    /**
     * @查看保存的区域内是否已存在详细自提地点
     * @param areaDetail
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/checkAreaDetail",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp checkAreaDetail(@RequestBody AreaDetail areaDetail){
        log.debug("查看保存的区域内是否已存在详细自提地点");
        AreaDetail areaDetailOld=areaDetailDao.findByAreaCode(areaDetail.getAreaCode());
        if(areaDetailOld!=null){
            return JsonResp.fa("该区域已存在详细自提地点,是否确定更换原有地址？");
        }
       return  JsonResp.ok();
    }

    /**
     * @更新自提地点详细
     * @param areaDetail
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/updateAreaDetail",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp updateAreaDetail(@RequestBody AreaDetail areaDetail){
        log.debug("更新自提地点详细");
        AreaDetail areaDetailOld=areaDetailDao.findByAreaCode(areaDetail.getAreaCode());
        areaDetailOld.setDetailPlace(areaDetail.getDetailPlace());
        areaDetailDao.save(areaDetailOld);
        return  JsonResp.ok();
    }
    /**
     * @查询详细自提地点
     * @param areaCode
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/loadData",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp loadData(String areaCode){
        log.debug("查询详细自提地点");
        AreaDetail areaDetail=areaDetailDao.findByAreaCode(areaCode);
        return  JsonResp.ok(areaDetail);
    }

    /**
     * 分页查询(jpa方式)
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageAreaDetailJpa( Integer pageNum,Integer pageSize) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = areaDetailDao.findAll(pageable) ;//无条件分页
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除-one(永久)
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteAreaDetailOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteAreaDetailOne(Long id) {
        log.debug("删除-one(永久)");
        areaDetailDao.delete(id);
        return JsonResp.ok();
    }
}
